package com.testmappend.test;

import com.testmappend.domain.College;
import org.hibernate.Cache;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

public class HibernateUtils {

    // 1.创建工厂对象;
    private static SessionFactory sessionFactory;

    // 2.初始化工厂对象;
    static {
        sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = getSession();
        Cache cache = sessionFactory.getCache();
        Query<College> query = session.createQuery("from College ", College.class);
        sessionFactory.addNamedQuery("allCollege",query);
    }

    // 3.获得Session;
    public static Session getSession() {

        return sessionFactory.openSession();

    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void setSessionFactory(SessionFactory sessionFactory) {
        HibernateUtils.sessionFactory = sessionFactory;
    }
}
